//
//  NSString+NSString_PinyinStr.m
//  Demo
//
//  Created by 龚碧容 on 2017/9/20.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "NSString+NSString_PinyinStr.h"

@implementation NSString (NSString_PinyinStr)
+(NSString *)transformToPinyin:(NSString *)aString{

    NSMutableString *str=[NSMutableString stringWithString:aString];
    //转变为可变字符串
    CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformMandarinLatin, NO);
    //转换为不带声调的拼音
    CFStringTransform((CFMutableStringRef)str, NULL, kCFStringTransformStripDiacritics, NO);
    
    NSArray *pinyinArray=[str componentsSeparatedByString:@" "];
    NSMutableString *allString=[NSMutableString new];
    
    int count=0;
    
    for (int i=0; i<pinyinArray.count; i++) {
        if (i==count) {
            [allString appendString:@"#"];
        }
        [allString appendFormat:@"%@",pinyinArray[i]];
    }
    [allString appendString:@","];
    count++;
    
    
    NSMutableString *initialString=[NSMutableString new];
    for (NSString *s in pinyinArray) {
        if (s.length>0) {
            [initialString appendString:[s substringToIndex:1]];
        }
    }
    
    [allString appendFormat:@"#%@",initialString];
    [allString appendFormat:@"#%@",aString];
 
    return allString;
}




@end
